package com.strawcom.charmring.bluetooth;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;

import com.strawcom.charmring.App;
import com.strawcom.charmring.util.LogUtil;

public class InfoRecorder {

	@SuppressLint("SimpleDateFormat")
	private SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
	private final LinkedList<String> connectInfo = new LinkedList<String>();

	public synchronized void recordConnectInfo(String info) {
		boolean donotRecord = true;
		if(donotRecord) {
			// 暂时不要记录数据
			return;
		}
		
		info = sdf.format(new Date()) + " " + info;
		connectInfo.addLast(info);
		if (connectInfo.size() > 50) {
			final StringBuffer sb = new StringBuffer();
			for (String i : connectInfo) {
				sb.append(i).append("\n");
			}
			savaInfoToSD(App.i(), "log_" + sdf.format(new Date()), sb.toString());
			connectInfo.clear();
		}
	}

	public static String savaInfoToSD(Context context, String filename, String info) {
		String fileName = null;
		StringBuffer sb = new StringBuffer();

		for (Map.Entry<String, String> entry : obtainSimpleInfo(context).entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			sb.append(key).append(" = ").append(value).append("\n");
		}

		sb.append(info);

		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File dir = new File(context.getExternalFilesDir(null), "blerecord");
			if (!dir.exists()) {
				dir.mkdir();
			}

			try {
				fileName = dir.toString() + File.separator + filename  + ".txt";
				LogUtil.e("蓝牙信息记录至文件：" + filename);
				FileOutputStream fos = new FileOutputStream(fileName);
				fos.write(sb.toString().getBytes());
				fos.flush();
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return fileName;
	}

	private static HashMap<String, String> obtainSimpleInfo(Context context) {
		HashMap<String, String> map = new HashMap<String, String>();
		PackageManager mPackageManager = context.getPackageManager();
		PackageInfo mPackageInfo = null;
		try {
			mPackageInfo = mPackageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		map.put("versionName", mPackageInfo.versionName);
		map.put("versionCode", "" + mPackageInfo.versionCode);

		map.put("MODEL", "" + Build.MODEL);
		map.put("SDK_INT", "" + Build.VERSION.SDK_INT);
		map.put("PRODUCT", "" + Build.PRODUCT);

		return map;
	}

	@SuppressLint("SimpleDateFormat") private static String paserTime(long milliseconds) {
		System.setProperty("user.timezone", "Asia/Shanghai");
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone.setDefault(tz);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		return format.format(new Date(milliseconds));
	}
}
